package src.model.gameWorld;

import java.util.ArrayList;

/**
 * <b>Overview</b><br>
 * <p>In orderto forward incoming contorller messages to the correct player,
 * the GameWorld must maintain a notaion of whom the current player is, and
 * be able to change from one player to the next when appropriate.</p>
 * <br><br>
 * <b>Responsibilites</b><br>
 * <p>The CurrentPlayer object is responsible for keeping track of which
 * player is currently playing, and managing the switching between players.
 * It is also responsible for forwarding certain messages to the current
 * player</p>
 * <br><br>
 * <b>Collaborators</b>
 * <ul>
 * <li>Player - Client</li>
 * <li>UpdateViewEventListener - Subcontractor</li>
 * </ul>
 * <br><br>
 * <b>Implementor</b> - Brent<br>
 * <b>Tester</b> - Tim<br>
 * 
 * @author Brent
 *
 */
public class CurrentPlayer
{
	private ArrayList<Player> myPlayers;
	private int myCurrentPlayerIndex;
	
	private UpdateViewEventListener myUpdateViewEL;
	
	/**
	 * Constructs a new CurrentPlayer object, using the provided players
	 * @param players - colleciton of players to be managed
	 */
	public CurrentPlayer(ArrayList<Player> players)
	{
		myPlayers = players;
		myCurrentPlayerIndex = 0;
		
		myUpdateViewEL = new UpdateViewEventListener(this);
		
	}
	
	/**
	 * Selects the next player, calling each Player's beginTurn() and 
	 * endTurn() where proper.
	 * 
	 * @return the new current player
	 */
	public Player SelectNextPlayer()
	{
		// end the old player's turn
		myPlayers.get(myCurrentPlayerIndex).endTurn();
		
		// select the next player
		do
		{
			myCurrentPlayerIndex++;
			myCurrentPlayerIndex %= myPlayers.size();
		}while(!myPlayers.get(myCurrentPlayerIndex).isValid());
		
		// begin the next player's turn
		myPlayers.get(myCurrentPlayerIndex).beginTurn();
		
		// alert the controller
		// TODO: event code here
		
		return myPlayers.get(myCurrentPlayerIndex);
	}
	
	/**
	 * Retreives a reference to the current Player object
	 * @return the current Plaeyr
	 */
	public Player getCurrentPlayer()
	{
		return myPlayers.get(myCurrentPlayerIndex);
	}

	/**
	 * Retreives a reference to the current UpdateView Event Listener.
	 * @return the current UpdateViewEventListener
	 */
	public UpdateViewEventListener getMyUpdateViewEL()
	{
		return myUpdateViewEL;
	}	
	
}
